package core;

import fileoperation.FileDeletion;
import repository.Repository;
import gitobject.*;
import java.io.File;
import java.io.IOException;
import sha1.SHA1;
public class JitHash {
    /**
     * Init repository in your working area.
     *
     * @param filename
     * @throws IOException
     */
    //功能入口，创建GitObject对象
    //输入file时在repository上生成一个blob对象，输入dir时在repository上生成一个tree对象
    //生成路径为worktree.gitDir/Objects

    public static void hash(String filename) throws IOException {
        File file = new File(filename);//文件对象file指向名为filename的文件
        if (!file.exists()) { //若文件不存在，输出"file not exists"
            System.out.println("file not exists");
            return;
        }
        String append = "";
        if (file.isDirectory()) {//若file为文件夹，则创建tree对象
            try{
                Tree tree = new Tree(file);
                append = tree.toString();
                tree.writeObject();
                //append = "040000 tree " + SHA1.getHash(file) + " " + file.getName();
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else {//若file为文件，则创建blob对象
           try {
               Blob blob = new Blob(file);
               blob.writeObject();
               //append = "100644 blob " + getHash(file) + " " + file.getName();
           }catch (Exception e) {
               e.printStackTrace();
           }
        }
    }

  /*  public static void main(String[] args) {
        try {
            String filename = "";
            new JitHash().hash(filename);
         //   hash(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
